﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NFinal.Middleware.Resources.Helper
{
    public static class ResourcesHelper
    {
        public static string[] textFileExtension = { ".html", ".css", ".txt", ".json", ".js", ".sass", ".less", ".ts", ".xml", ".htm" };
        public static bool IsTextResouce(string extension)
        {
            return textFileExtension.Contains(extension);
        }
        /// <summary>
        /// 比较输入字符串与模式的是否匹配，将使用指针根据每个字符进行比较
        /// </summary>
        /// <param name="input">输入字符串</param>
        /// <param name="pattern">模式，允许使用的通配符：?，*，其中? 代表任意一个字符，* 代表零或多个任意字符</param>
        /// <returns></returns>
        public static unsafe Boolean IsMatchByPtr(String input, String pattern)
        {

            Boolean matched = false;
            fixed (char* p_wild = pattern)
            fixed (char* p_str = input)
            {
                char* wild = p_wild;
                char* str = p_str;
                char* cp = null;
                char* mp = null;
                while ((*str) != 0 && (*wild != '*'))
                {
                    if ((*wild != *str) && (*wild != '?'))
                    {
                        return matched;
                    }
                    wild++;
                    str++;
                }
                while (*str != 0)
                {
                    if (*wild == '*')
                    {
                        if (0 == (*++wild))
                        {//如果*后面没有其它模式符，则判定匹配
                            matched = true;
                            return matched;
                        }
                        mp = wild;
                        cp = str + 1;
                    }
                    else if ((*wild == *str) || (*wild == '?'))
                    {
                        wild++;
                        str++;
                    }
                    else
                    {
                        //模式串未到结尾，而输入字串已经走到结尾，判定不匹配
                        wild = mp;//冻结，固定在不匹配的模式字符上
                        str = cp++;
                    }
                }

                //修正模式串
                while (*wild == '*')
                {
                    wild++;
                }
                return (*wild) == 0 ? true : false;
            }
        }//end mehtod
    }
}
